package com.tydic.daily.utils;

import org.apache.commons.lang.StringUtils;

/**
 * String Util
 * 
 */
public class StringUtil {

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 *            null、""、"null"都返回true
	 * @return
	 */
	public static boolean isNullString(String str) {
		return (null == str || StringUtils.isBlank(str.trim()) || "null".equals(str.trim().toLowerCase())) ? true : false;
	}

	/**
	 * 格式化字符串 如果为空，返回""
	 * 
	 * @param str
	 * @return
	 */
	public static String formatString(String str) {
		if (isNullString(str)) {
			return "";
		} else {
			return str;
		}
	}

	/**
	 * 判断是否是整数类型
	 * 
	 * @param value
	 * @return
	 */
	public static boolean isInteger(String value) {
		if (value == null) {
			return false;
		}
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isLong(String value) {
		if (value == null) {
			return false;
		}
		try {
			Long.parseLong(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isShort(String value) {
		if (value == null) {
			return false;
		}
		try {
			Short.parseShort(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 获取中文字符串每个汉字的首字母
	 * 
	 * @param getstring
	 * @return
	 */
	public static String convertZH(String getstring) {

		String backgetchar = "";// 用户输入汉字的拼音简码
		if (StringUtil.isChinese(getstring)) {
			String str[] = getstring.split("");// 将字符串转换成数组,数组第0位为""
			int length = str.length;// 获得数组长度
			int start = 1;// 设置循环初始值
			byte[] a = null;
			String getchar = "";// 存储汉字拼音首字母
			while (start < length) {
				a = str[start].getBytes();
				if (a.length > 1) {
					int asc = 256 * (a[0] + 256) + (a[1] + 256);
					if (asc >= 45217 && asc <= 45252) {
						getchar = "A";
					} else if (asc >= 45253 && asc <= 45760) {
						getchar = "B";
					} else if (asc >= 45761 && asc <= 46317) {
						getchar = "C";
					} else if (asc >= 46318 && asc <= 46825) {
						getchar = "D";
					} else if (asc >= 46826 && asc <= 47009) {
						getchar = "E";
					} else if (asc >= 47010 && asc <= 47296) {
						getchar = "F";
					} else if (asc >= 47297 && asc <= 47613) {
						getchar = "G";
					} else if (asc >= 47614 && asc <= 48118) {
						getchar = "H";
					} else if (asc >= 48119 && asc <= 49061) {
						getchar = "J";
					} else if (asc >= 49062 && asc <= 49323) {
						getchar = "K";
					} else if (asc >= 49324 && asc <= 49895) {
						getchar = "L";
					} else if (asc >= 49896 && asc <= 50370) {
						getchar = "M";
					} else if (asc >= 50371 && asc <= 50613) {
						getchar = "N";
					} else if (asc >= 50614 && asc <= 50621) {
						getchar = "O";
					} else if (asc >= 50622 && asc <= 50905) {
						getchar = "P";
					} else if (asc >= 50906 && asc <= 51386) {
						getchar = "Q";
					} else if (asc >= 51387 && asc <= 51445) {
						getchar = "R";
					} else if (asc >= 51446 && asc <= 52217) {
						getchar = "S";
					} else if (asc >= 52218 && asc <= 52967) {
						getchar = "T";
					} else if (asc >= 52698 && asc <= 52979) {
						getchar = "W";
					} else if (asc >= 52980 && asc <= 53640) {
						getchar = "X";
					} else if (asc >= 53689 && asc <= 54480) {
						getchar = "Y";
					} else if (asc >= 54481 && asc <= 62289) {
						getchar = "Z";
					}
				} else {
					getchar = str[start];
				}
				backgetchar += getchar;
				start++;
			}
		} else {
			String[] initc = getstring.split(" ");
			for (int i = 0; i < initc.length; i++) {
				backgetchar += initc[i].substring(0, 1);
			}
		}

		return backgetchar;
	}

	public static boolean isChinese(String str) {
		boolean flag = false;
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if (!(ch > 0x7e || ch < 0x21)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param stringInput
	 *            要进行判断的字符串。
	 * @return 如果非空返回True，否则返回False。
	 */
	public static boolean isDigit(String stringInput) {
		if (stringInput == null)
			return false;
		if ("".endsWith(stringInput))
			return false;
		char[] times = stringInput.trim().toCharArray();
		for (int i = 0; i < times.length; i++) {
			char ch = times[i];
			if (false == Character.isDigit(ch)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param stringInput
	 *            要进行判断的字符串。
	 * @return 如果非空返回True，否则返回False。
	 */
	public static boolean isDigit(int stringInput) {
		if (stringInput >= 0)
			return true;
		return false;
	}

	public static boolean isChineseRex(String str) {

		boolean flag = false;
		if (str == null || "".equals(str)) {
			flag = true;
		}
		String rexStr = str.replaceAll("[^\\u4e00-\\u9fa5]", "");
		if (rexStr.length() != str.length()) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 将指定的数字转化为指定长度的字符串，多余部分用"#"填充。例如：intToStrWithSharp(1000, 6)->"##1000"
	 * 
	 * @param value
	 *            要转换的整数
	 * @param length
	 *            转换后的字符串长度
	 * @return 转换后的字符串，如果指定的长度小于整数的位数，则只返回数字。例如：intToStrWithSharp(1000,
	 *         2)->"1000"
	 */
	public static String intToStrWithSharp(Integer value, int length, char c) {
		int valueLength = value.toString().length();
		int diff = length - valueLength;

		if (value.intValue() < Integer.MAX_VALUE) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < diff; i++) {
				sb.append(c);
			}
			sb.append(value.intValue());
			return sb.toString();
		} else {
			return "-1";
		}
	}

}
